//1. 安装 npm i vue-router

//2. 引入 以前是在main.js完成的，但是路由相关代码会比较多，现在是在src/router/index.js 进行引入和配置。
import VueRouter from 'vue-router'
//3.注册
import Vue from 'vue'
Vue.use(VueRouter)
//4.初始化
//导入组件
import Login from '@/views/login'
const router = new VueRouter({
  routes: [{
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: Layout,
      children: [{
        path: "/",
        component: Home
      },
        { path: '/article', component: Article },
        {path: '/publish', component: Publish }
      ]
    }
  ]
})
import Layout from '@/views/Layout'
import Home from '@/views/home'
import Article from '@/views/article'
import Publish from '@/views/publish'

//5.导出路由实例
export default router

//导航守卫
import auth from '@/utils/auth'
router.beforeEach((to, from, next) => {
  //to and from are Route Object,next() must be called to resolve the hook}
  //1. 如果未登录,访问的不是登录页面(访问的后台页面),拦截到登录页面
  //2. 其他情况一概放行
  const token = auth.getToken()
  if (!token && to.path !== "/login") {
    next('/login')
  } else {
    next()
  }

})
